/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.maestros;

import DAO.ParametroDAO;
import Model.Parametro;
import Model.Vehiculo;
import Presenter.EnvioPedidoPresenter;
import Presenter.VehiculoPresenter;
import Principals.Utils;
import Principals.Validadores;
import View.components.Index;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;

/**
 *
 * @author walter
 */
public class Frm_ModificarVehiculo extends javax.swing.JDesktopPane {

    /**
     * Creates new form Frm_BuscarVehiculo
     */
    Vehiculo v = new Vehiculo();
    Validadores val = new Validadores();

    public Frm_ModificarVehiculo(int idVehiculo) {
        initComponents();
        EnvioPedidoPresenter.actualizarEnvioPedido();
        val.desactivarAccionPegar(txtPlaca);
        //Carga campos
        List<Parametro> lstTipoVehiculo = ParametroDAO.getTipoVehiculos();
        for (Parametro p : lstTipoVehiculo) {
            cbxTipoVehiculo.addItem(p);
        };
        
        //Carga datos
        v = VehiculoPresenter.cargarVehiculo(idVehiculo);
        cbxAnhoFabricacion.setYear(v.getAnho());
        txtPlaca.setText(v.getPlaca());
        cbxTipoVehiculo.setSelectedItem(v.getTipoVehiculo());
        
        if (v.getEstado()==2) {
            cbxEstado.addItem("En ruta");
            cbxEstado.setSelectedItem("En ruta");
            cbxEstado.setEnabled(false);
        } else {
            String[] estados = Utils.getEstadosMaestro();
            for (String e : estados) {
                if (!(e.equals("Eliminado") || e.equals("En ruta"))){
                    cbxEstado.addItem(e);
                }
            }
            if (v.getEstado()==1) cbxEstado.setSelectedItem("Disponible");
            if (v.getEstado()==3) cbxEstado.setSelectedItem("En mantenimiento");
            if (v.getEstado()==4) cbxEstado.setSelectedItem("Malogrado");
    }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar2 = new javax.swing.JToolBar();
        btnToolNuevo = new javax.swing.JButton();
        btnToolModificar = new javax.swing.JButton();
        btnToolAlmacenar = new javax.swing.JButton();
        btnToolEliminar = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnToolLimpar = new javax.swing.JButton();
        btnToolBuscar = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        btnToolCancelar = new javax.swing.JButton();
        pnlBuscarEmpleado = new javax.swing.JPanel();
        lblTipoVehiculo = new javax.swing.JLabel();
        lblPlaca = new javax.swing.JLabel();
        txtPlaca = new javax.swing.JTextField();
        cbxTipoVehiculo = new javax.swing.JComboBox();
        lblAnhoFabricacion = new javax.swing.JLabel();
        cbxAnhoFabricacion = new com.toedter.calendar.JYearChooser();
        lblEstado = new javax.swing.JLabel();
        cbxEstado = new javax.swing.JComboBox();

        setPreferredSize(new java.awt.Dimension(665, 435));

        jToolBar2.setFloatable(false);
        jToolBar2.setBorderPainted(false);

        btnToolNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Nuevo.png"))); // NOI18N
        btnToolNuevo.setEnabled(false);
        btnToolNuevo.setFocusable(false);
        btnToolNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolNuevoActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolNuevo);

        btnToolModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Editar.png"))); // NOI18N
        btnToolModificar.setEnabled(false);
        btnToolModificar.setFocusable(false);
        btnToolModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolModificarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolModificar);

        btnToolAlmacenar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Guardar.png"))); // NOI18N
        btnToolAlmacenar.setFocusable(false);
        btnToolAlmacenar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolAlmacenar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolAlmacenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolAlmacenarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolAlmacenar);

        btnToolEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Eliminar.png"))); // NOI18N
        btnToolEliminar.setEnabled(false);
        btnToolEliminar.setFocusable(false);
        btnToolEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolEliminarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolEliminar);
        jToolBar2.add(jSeparator3);

        btnToolLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Limpiar.png"))); // NOI18N
        btnToolLimpar.setEnabled(false);
        btnToolLimpar.setFocusable(false);
        btnToolLimpar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolLimpar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolLimparActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolLimpar);

        btnToolBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Buscar.png"))); // NOI18N
        btnToolBuscar.setFocusable(false);
        btnToolBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolBuscarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolBuscar);
        jToolBar2.add(jSeparator4);

        btnToolCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Cancelar.png"))); // NOI18N
        btnToolCancelar.setToolTipText("");
        btnToolCancelar.setFocusable(false);
        btnToolCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolCancelarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolCancelar);

        pnlBuscarEmpleado.setBorder(javax.swing.BorderFactory.createTitledBorder("Modificar datos de vehículo"));

        lblTipoVehiculo.setText("Tipo de Vehículo*:");

        lblPlaca.setText("Placa*:");

        txtPlaca.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPlacaKeyTyped(evt);
            }
        });

        lblAnhoFabricacion.setText("Año de fabricación:");

        lblEstado.setText("Estado:");

        javax.swing.GroupLayout pnlBuscarEmpleadoLayout = new javax.swing.GroupLayout(pnlBuscarEmpleado);
        pnlBuscarEmpleado.setLayout(pnlBuscarEmpleadoLayout);
        pnlBuscarEmpleadoLayout.setHorizontalGroup(
            pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTipoVehiculo)
                    .addComponent(lblPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtPlaca)
                    .addComponent(cbxTipoVehiculo, 0, 144, Short.MAX_VALUE))
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblAnhoFabricacion)
                        .addGap(18, 18, 18))
                    .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(lblEstado)
                        .addGap(65, 65, 65)))
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbxAnhoFabricacion, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47))
        );
        pnlBuscarEmpleadoLayout.setVerticalGroup(
            pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                        .addComponent(cbxAnhoFabricacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cbxEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlBuscarEmpleadoLayout.createSequentialGroup()
                        .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTipoVehiculo)
                            .addComponent(cbxTipoVehiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblAnhoFabricacion))
                        .addGap(18, 18, 18)
                        .addGroup(pnlBuscarEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPlaca)
                            .addComponent(txtPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblEstado))))
                .addContainerGap(278, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlBuscarEmpleado, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pnlBuscarEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cambiarVista(JDesktopPane panel) {
        ((Index) (this.getTopLevelAncestor())).changeDesktopPane(panel);
    }

    private void btnToolNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolNuevoActionPerformed
        cambiarVista(new Frm_RegistrarVehiculo()); // <---------------------------- ESTO SE DEBE CAMBIAR
    }//GEN-LAST:event_btnToolNuevoActionPerformed

    private void btnToolModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolModificarActionPerformed
    }//GEN-LAST:event_btnToolModificarActionPerformed

    private void btnToolBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolBuscarActionPerformed
        cambiarVista(new Frm_BuscarVehiculo());
    }//GEN-LAST:event_btnToolBuscarActionPerformed

    private void btnToolEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolEliminarActionPerformed
        cambiarVista(new Frm_EliminarVehiculo());
    }//GEN-LAST:event_btnToolEliminarActionPerformed

    private void btnToolCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolCancelarActionPerformed
        cambiarVista(new Frm_BuscarVehiculo());
    }//GEN-LAST:event_btnToolCancelarActionPerformed

    private void btnToolAlmacenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolAlmacenarActionPerformed
        String placa = txtPlaca.getText();
        Parametro tipo_vehiculo = (Parametro) cbxTipoVehiculo.getSelectedItem();
        int anho = cbxAnhoFabricacion.getYear();
        int estado=0;
        
        if (cbxEstado.getSelectedItem().toString().equals("Disponible")) estado=1;
        if (cbxEstado.getSelectedItem().toString().equals("En mantenimiento")) estado=3;
        if (cbxEstado.getSelectedItem().toString().equals("Malogrado")) estado=4;

        //VehiculoPresenter.modificarVehiculo(idV, placa, tipo_vehiculo, anho);
        if (VehiculoPresenter.verificarPlacaModificar(v.getIdVehiculo(),placa) <= 0) {
            if (!"".equals(placa)) {
                VehiculoPresenter.modificarVehiculo(v, placa, tipo_vehiculo, anho, estado);
                JOptionPane.showMessageDialog(this, "Se actualizó el vehículo de placa " + placa);
                cambiarVista(new Frm_BuscarVehiculo());
            } else {
                JOptionPane.showMessageDialog(this, "Debe completar todos los campos obligatorios");
            }
        } else {
            JOptionPane.showMessageDialog(this, "La placa ingresada ya se encuentra registrada en el sistema");
        }
    }//GEN-LAST:event_btnToolAlmacenarActionPerformed

    private void btnToolLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolLimparActionPerformed
        cbxTipoVehiculo.setSelectedIndex(0);
        cbxAnhoFabricacion.setYear(2000);
        txtPlaca.setText("");
    }//GEN-LAST:event_btnToolLimparActionPerformed

    private void txtPlacaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPlacaKeyTyped
        val.validaPlaca(evt);
        val.validaMaxSize(evt, 9, txtPlaca.getText());
    }//GEN-LAST:event_txtPlacaKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnToolAlmacenar;
    private javax.swing.JButton btnToolBuscar;
    private javax.swing.JButton btnToolCancelar;
    private javax.swing.JButton btnToolEliminar;
    private javax.swing.JButton btnToolLimpar;
    private javax.swing.JButton btnToolModificar;
    private javax.swing.JButton btnToolNuevo;
    private com.toedter.calendar.JYearChooser cbxAnhoFabricacion;
    private javax.swing.JComboBox cbxEstado;
    private javax.swing.JComboBox cbxTipoVehiculo;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JLabel lblAnhoFabricacion;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JLabel lblPlaca;
    private javax.swing.JLabel lblTipoVehiculo;
    private javax.swing.JPanel pnlBuscarEmpleado;
    private javax.swing.JTextField txtPlaca;
    // End of variables declaration//GEN-END:variables
}
